#include "mywidget.h"
#include "iostream"

using namespace std;
MyWidget::MyWidget(QWidget *parent) :
    QGLWidget(parent)
{
    red = 0.0;
    green = 0.0;
    blue = 0.0;
}

void MyWidget::initializeGL(){
    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 50.0, 0.0, 50.0);
    glMatrixMode(GL_MODELVIEW);
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
}

void MyWidget::resizeGL(int w, int h){
    glViewport(0, 0, w, h);
}

void MyWidget::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT);
    drawSierpisnkiTriangle();
}

void MyWidget::drawSierpisnkiTriangle(){
    GLfloat vertices[3][3] = {{0.0, 0.0, 0.0},
                              {25.0, 50.0, 0.0},
                              {50.0, 0.0, 0.0}};
    GLfloat p[3]= {7.5, 5.0, 0.0};
    int j, k;
    glBegin(GL_POINTS);
        glColor3f(red,green,blue);
        for ( k = 0; k < points; k++){
            j = random()%3;
            p[0] = (p[0] + vertices[j][0]) / 2;
            p[1] = (p[1] + vertices[j][1]) / 2;
            glVertex3fv(p);
        }
    glEnd();
    glFlush();
}

void MyWidget::clear(){
    points = 0;
    updateGL();
}

void MyWidget::updateRedColor(int color){
    red = (float) color/100;
    updateGL();
}

void MyWidget::updateBlueColor(int color){
    blue = (float) color/100;
    updateGL();
}

void MyWidget::updateGreenColor(int color){
    green = (float) color/100;
    updateGL();
}

void MyWidget::setPoints(int points){
    this->points = points;
}

void MyWidget::draw(){
    updateGL();
}
